<?php

namespace app\admin\model\litestore;

use think\Db;
use think\Model;

class Litestoreorder extends Model
{
    // 表名
    protected $name = 'litestore_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'pay_time_text',
        'freight_time_text',
        'receipt_time_text',
        'order_status_text'
    ];
    

    
    public function getPayStatusList()
    {
        return ['10' => __('Pay_status 10'),'20' => __('Pay_status 20')];
    }     

    public function getFreightStatusList()
    {
        return ['10' => __('Freight_status 10'),'20' => __('Freight_status 20')];
    }     

    public function getReceiptStatusList()
    {
        return ['10' => __('Receipt_status 10'),'20' => __('Receipt_status 20')];
    }     

    public function getOrderStatusList()
    {
        return ['10' => __('Order_status 10'),'20' => __('Order_status 20'),'30' => __('Order_status 30')];
    }     


    public function getPayStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }



    public function getFreightTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['freight_time']) ? $data['freight_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }





    public function getReceiptTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['receipt_time']) ? $data['receipt_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getOrderStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
        $list = $this->getOrderStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPayTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setFreightTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setReceiptTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function address()
    {
        return $this->hasOne('Litestoreorderaddress', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods()
    {
        return $this->hasMany('Litestoreordergoods','order_id','id');
    }
    public function user()
    {
        return $this->belongsTo('app\admin\model\User');
    }

    public function addOrder($uid,$data = array()){

        Db::startTrans();
        $id = $this->insertGetId([
            'user_id' => $uid,
            'order_no' => $data['orderNo'],
            'seller_id' => $data['seller_id'],
            'total_price' => $data['amount'],
            'pay_price' =>  $data['pay_price']/100,
            'createtime' =>time(),
            'express_price' =>0,
            'ratio'=>$data['ratio']
        ]);

        $litestoregoods = new Litestoregoods();
        $goods = $data['goods'];
        foreach ($goods as $key=>$val){
            $goods = $litestoregoods->where('goods_id',$val['goods_id'])->find();

            $goodsList[] = [
                'user_id' => $uid,
                'goods_id' => $goods['goods_id'],
                'goods_name' =>  $goods['goods_name'],
                'goods_price' => $val['price'],
                'images' =>  $goods['images'],
                'deduct_stock_type' => $goods['deduct_stock_type'],
                'spec_type' =>  $goods['spec_type'],
                'total_num' => $val['count'],
                'goods_spec_id' =>0 ,
                'spec_sku_id' =>$val['skuId'] ,
                'total_price' => $val['totalPrice']/100,
                'order_id' => $id
            ];
        }
        $lite = new Litestoreordergoods();
        $lite->insertAll($goodsList);

        $liteorderAddress = new Litestoreorderaddress();
        $liteorderAddress->save([
            'user_id' => $uid,
            'name' => $data['address']['username'],
            'phone' => $data['address']['mobile'],
            'province_id' => $data['address']['provinceid'],
            'city_id' => $data['address']['cityid'],
            'region_id' => $data['address']['areaid'],
            'detail' => $data['address']['address'],
            'order_id' => $id
        ]);
        Db::commit();

        return $id;

    }


    public function addPinOrder($uid,$data = array()){

        Db::startTrans();
        $id = $this->insertGetId([
            'user_id' => $uid,
            'order_no' => $data['orderNo'],
            'seller_id' => $data['seller_id'],
            'total_price' => $data['amount']/100,
            'pay_price' =>  $data['amount']/100,
            'createtime' =>time(),
            'pinid' =>$data['pinid'],
            'express_price' =>0,
            'ratio'=>$data['ratio']
        ]);
        $litestoregoods = new Litestoregoods();
        $goods = $litestoregoods->where('goods_id',$data['goods']['goods_id'])->find();
        $goodsList[] = [
            'user_id' => $uid,
            'goods_id' => $goods['goods_id'],
            'goods_name' =>  $goods['goods_name'],
            'goods_price' => $data['goods']['goods_price'],
            'images' =>  $goods['images'],
            'deduct_stock_type' => $goods['deduct_stock_type'],
            'spec_type' =>  $goods['spec_type'],
            'total_num' => $data['num'],
            'total_price' => $data['amount']/100,
            'order_id' => $id
        ];
        $lite = new Litestoreordergoods();
        $lite->insertAll($goodsList);

        $liteorderAddress = new Litestoreorderaddress();
        $liteorderAddress->save([
            'user_id' => $uid,
            'name' => $data['address']['username'],
            'phone' => $data['address']['mobile'],
            'province_id' => $data['address']['provinceid'],
            'city_id' => $data['address']['cityid'],
            'region_id' => $data['address']['areaid'],
            'detail' => $data['address']['address'],
            'order_id' => $id
        ]);
        Db::commit();

        return $id;

    }





}
